package com.example.market_management.sheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.market_management.mapper.orderMapper;
import com.example.market_management.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Configuration
@Slf4j
@EnableScheduling
@Transactional(rollbackFor = {Exception.class})
public class orderScheduled {
     @Autowired
     private orderMapper orderMapper;


     @Scheduled(fixedRate = 300000)
     private void  updateOrderStatus(){
          log.info("执行订单状态更新任务");
          QueryWrapper queryWrapper=new QueryWrapper();
          List<Order> orderList=orderMapper.selectList(queryWrapper);
          for(Order order:orderList) {
               Date delivery_time = order.getDeliveryTime();
               Date startTime = order.getStartTime();
               Date nowDate = new Date();
               int orderStatus = order.getOrderStatus();
               String orderId = order.getOrderId();
               //更新为已收货
               if (delivery_time.getTime() - nowDate.getTime() <= 6000 * 30) {

                    if (orderStatus != 2) {
                         orderMapper.updateOrderStatus(orderId, 2);
                         log.info("更新订单状态为已收货:" + orderId);
                    }
               }
               //更新为已发货
               else if (startTime.getTime() - nowDate.getTime() <= 6000 * 30) {
                    if (orderStatus != 1) {
                         orderMapper.updateOrderStatus(orderId, 1);
                         log.info("更新订单状态为已发货" + orderId);
                    }
               }
          }

     }


}
